package cn.ulc.socket.admin.service.member.impl;

import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.constant.enums.DeletedStatus;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.socket.admin.common.BaseService;
import cn.ulc.socket.admin.model.dto.member.MemberDTO;
import cn.ulc.socket.admin.model.dto.member.MemberListDTO;
import cn.ulc.socket.admin.model.param.member.MemberSaveParam;
import cn.ulc.socket.admin.service.member.MemberServ;
import cn.ulc.store.dao.user.MemberDao;
import cn.ulc.store.entity.Company;
import cn.ulc.store.entity.staff.Staff;
import cn.ulc.store.entity.user.Member;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MemberServImpl extends BaseService implements MemberServ {
    @Autowired
    MemberDao memberDao;

    @Override
    public MemberListDTO findList(String key, PaginationParam param) {
        Staff loginUser = getCurrentUser();
        Map<String,Object> condition = new HashMap<>();
        condition.put("key",key);
        condition.put("companyId",loginUser.getCompanyId());
        PageQuery pageQuery = initPageQuery(param);
        pageQuery.setParas(condition);
        memberDao.findListPage(pageQuery);
        return new MemberListDTO(pageQuery.getList(),pageQuery.getTotalRow());
    }

    @Override
    public MemberDTO findDetail(Long id) {
        Member member = memberDao.findOne(id);
        if(member==null) throw new PlatformException("获取会员信息失败!");
        return new MemberDTO(member);
    }

    @Override
    public Boolean save(MemberSaveParam param){
        Staff loginUser = getCurrentUser();
        if(loginUser==null) throw new PlatformException("请重新登录!");
        Member checkPhone = memberDao.findByPhone(loginUser.getId(),param.getPhone());
        if(checkPhone!=null) throw new PlatformException("手机号已被使用!");
        Member member = new Member();
        setBaseInfo(member);
        member.setCompanyId(loginUser.getCompanyId());
        member.setName(param.getName());
        member.setCardNo(param.getCardNo());
        member.setPhone(param.getPhone());
        member.setBirthday(param.getBirthday());
        member.setAddress(param.getAddress());
        member.setStatus(AvailableStatus.AVAILABLE);
        member.setBalance(BigDecimal.ZERO);
        member.setPoints(0);
        memberDao.save(member);
        return true;
    }

    @Override
    public Boolean update(Long id, MemberSaveParam param) {
        Member member = memberDao.findOne(id);
        if(member==null) throw new PlatformException("获取会员信息失败!");
        if(!member.getPhone().equals(param.getPhone())){
            Member checkPhone = memberDao.findByPhone(member.getCompanyId(),param.getPhone());
            if(checkPhone!=null) throw new PlatformException("手机号已被使用!");
        }
        setBaseInfo(member);
        member.setName(param.getName());
        member.setPhone(param.getPhone());
        member.setBirthday(param.getBirthday());
        member.setAddress(param.getAddress());
        memberDao.save(member);
        return true;
    }

    @Override
    public Boolean deleted(Long id) {
        Member member = memberDao.findOne(id);
        if(member==null) throw new PlatformException("获取会员信息失败!");
        setBaseInfo(member);
        member.setDeleted(DeletedStatus.DELETED);
        memberDao.save(member);
        return true;
    }

    @Override
    public Map<String, Object> findStat(Long companyId, String beginDate, String endDate) {
        Map<String,Object>  map = memberDao.findStat(companyId,beginDate,endDate,AvailableStatus.AVAILABLE);
        return map;
    }

    @Override
    public Map<String, Object> findTodayOrderStat(Long companyId) {
        return memberDao.findTodayOrderStat(companyId);
    }

    @Override
    public List<Member> findList() {
        Company company = getCurrentCompany();
        return memberDao.findList(company.getId());
    }
}
